{{!
# Copyright (C) 2011 - present Instructure, Inc.
#
# This file is part of Canvas.
#
# Canvas is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the Free
# Software Foundation, version 3 of the License.
#
# Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
# details.
#
# You should have received a copy of the GNU Affero General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.
}}

<div role="region" aria-label="{{#t}}Question{{/t}}" class="quiz_sortable question_holder {{group}}">
  <div style="display: block; height: 1px; overflow: hidden;">&nbsp;</div>
  <a name="question_{{id}}"></a>
  <div class="display_question question {{type}} {{classes}}" id="question_{{id}}">
    <div class="header">
      <span class="name question_name" role="heading" aria-level="2">{{name}}</span>
      <span class="question_points_holder">
      {{#answered}}
        <div class="user_points">
            <% if hash_get(user_answer, :correct) == "undefined" %>
              {{#t}}Not yet graded{{/t}}
            <% else %>
                <%= render_score(hash_get(user_answer, :points)) %>
            <% end %>
          <%= t(:points_possible, "%{points_possible} pts", :points_possible => raw("<span class=\"points question_points\"> / #{round_if_whole(hash_get(question, :points_possible, "0"))}</span>")) %>
        </div>
      {{/answered}}
      {{^answered}}
	{{#points_possible}}
		{{points_possible}} {{#t}}pts{{/t}}
	{{/points_possible}}
      {{/answered}}
    </span>
    </div>
    <div class="text">
      <div id="question_{{id}}_question_text" class="question_text user_content">		
	{{#question}}
		{{> views/quizzes/quizzes/quiz_part}}
	{{/question}}
      </div>
      <div class="answers" style="display:none"> {{! TODO: Implement answer viewing }}
          <% if user_answer && question && question_type.multiple_sets %>
            <% variables.each_with_index do |variable, idx| %>
              <% answers = hash_get(question, :answers, []).select{|a| hash_get(a, :blank_id) == variable } %>
              <% user_answer[:text] = hash_get(user_answer, "answer_for_#{variable}")
                 user_answer[:text] = nil if user_answer[:text].blank?
                 user_answer[:answer_id] = hash_get(user_answer, "answer_id_for_#{variable}")
                 user_answer[:answer_id] = nil if user_answer[:answer_id].blank?
                 matched_answer = answers.find{|a| hash_get(a, :text) == hash_get(user_answer, :text) || hash_get(a, :id) == hash_get(user_answer, :answer_id) }
              %>
              <div class="answer_group">
                <b class="answer-group-heading"><%= before_label(:for_answer_number, "Answer %{answer_number}", :answer_number => idx + 1) %></b>
                <% if question_type.entry_type == "text_box" %>
                  <% answer_classes = ['selected_answer']
                     if display_correct_answers
                       answer_classes << ((matched_answer && matched_answer[:weight] == 100) ? 'correct_answer' : 'wrong_answer')
                     end
                     answer_classes << 'no_answer' if hash_get(user_answer, :text).blank?
                  %>
                  <div class='answer <%= answer_classes.compact.join(' ') %>'>
                    <%= hash_get(matched_answer || user_answer, :text) || t(:you_left_this_blank, "(You left this blank)") %>
                    <% if matched_answer && comment_get(matched_answer, :comments).present? %>
                      <div class="hide_right_arrow <%= "correct_answer" if display_correct_answers %>">
                        <div class="quiz_comment">
                          <%= comment_get(matched_answer, :comments) %>
                        </div>
                      </div>
                      <div class="clear"></div>
                    <% end %>
                  </div>
		  {{#display_answer}}
                    {{> views/quizzes/quizzes/display_answer}}
		  {{/display_answer}}
                <% else %>
                  <% if user_answer[:answer_id].nil? %>
                    <div class='answer selected_answer wrong_answer no_answer'>
                      {{#t}}(You left this blank){{/t}}
                    </div>
                  <% end %>
		  {{#display_answer}}
                    {{> views/quizzes/quizzes/display_answer}}
		  {{/display_answer}}
                <% end %>
              </div>
            <% end %>
          <% else %>
            <% if user_answer && question_type.display_answers == "single" %>
              <div>
                <% if question_type.entry_type == 'textarea' %>
                  {{#t}}Your Answer:{{/t}}
                  <div class="user_content quiz_response_text"><%= hash_get(user_answer, :text).html_safe %></div>
                <% else %>
                  <% matched_answer = hash_get(question, :answers, []).find{|a| hash_get(a, :text) == hash_get(user_answer, :text) || hash_get(a, :id) == hash_get(user_answer, :answer_id) } %>
                  <% hash_get(question, :answers, []).each do |a|
                    # Temporary workaround until we can figure out why the
                    # "answers" aren't getting cleared out when switching
                    # question types
                    break if hash_get(question, :question_type) == 'file_upload_question'
                    val = hash_get(user_answer, :text).to_f
                    # Duplicates logic from app/models/quizzes/quiz_question/numerical_question.rb:46
                    matched_answer = a if hash_get(a, :numerical_answer_type) == 'exact_answer' && val <= (hash_get(a, :exact) + hash_get(a, :margin, 0)) && val >= (hash_get(a, :exact) - hash_get(a, :margin, 0))
                    matched_answer = a if hash_get(a, :numerical_answer_type) == 'range_answer' && val <= hash_get(a, :end) && val >= hash_get(a, :start)
                    if hash_get(a, :numerical_answer_type) == 'precision_answer'
                      submission = BigDecimal.new(val, 16).split
                      expected = BigDecimal.new(a[:approximate].to_s, 16).split
                      precision = a[:precision].to_i
                      matched_answer = a if submission[0] == expected[0] && submission[1][0, precision] == expected[1][0, precision] && submission[3] == expected[3]
                    end
                  end %>
                  <% answer_classes = ['selected_answer']
                    if display_correct_answers && hash_get(question,:question_type) != 'file_upload_question'
                     answer_classes << ((hash_get(user_answer, :correct) == true) ? 'correct_answer' : 'wrong_answer')
                   end
                   answer_classes << 'no_answer' if hash_get(user_answer, :text).blank? || hash_get(user_answer, :attachment_ids).blank?
                  %>
                  <div class='answer <%= answer_classes.compact.join(' ') %>'>
                    <%= render :partial => "quizzes/quizzes/single_answer", :locals =>
                      {:question => question, :right_answers => right_answers, :question_type => question_type, :user_answer => user_answer} %>
                  </div>
                  <% if matched_answer && comment_get(matched_answer, :comments).present? %>
                    <div class="hide_right_arrow <%= "correct_answer" if display_correct_answers %>">
                      <div class="quiz_comment">
                        <%= comment_get(matched_answer, :comments) %>
                      </div>
                    </div>
                    <div class="clear"></div>
                  <% end %>
                <% end %>
              </div>
            <% end %>
            <% question[:answers] = [] if editing && question && hash_get(question, :question_type) == 'calculated_question' %>
            <% if question && (user_answer || assessment_results || !@quiz || display_correct_answers || (!assessment_results && !user_answer)) %>
              <% show_headers = @quiz && display_correct_answers && user_answer && question_type.display_answers == "single" && hash_get(question, :question_type) != "essay_question" %>
              <% if show_headers || !question_type.entry_type.match(/text_box/) %>
                <div class="answers_wrapper">
	           {{#display_answer}}
                      {{> views/quizzes/quizzes/display_answer}}
		   {{/display_answer}}
                </div>
              <% end %>
            <% end %>
          <% end %>
      </div>
    </div>
      {{comment}}
	{{#additional_comment}}
		<div class="quiz_comment">
		    {{additional_comment}}
		</div>
	{{/additional_comment}}
    <div class="clear"></div>
  </div>
</div>
